//
//  FPLuckyDrawAlertView.m
//  FootPad
//
//  Created by 张浩 on 2021/5/28.
//  Copyright © 2021 hb_147. All rights reserved.
//

#import "MM_CodeResultIncreaseDecreaseAlertView.h"
#import "AppDelegate.h"
#import "Custom_PlaceholderTextView.h"
#import "TZImagePickerController.h"
#import "MM_UploadImageModel.h"
#import "MM_UploadPicView.h"
#import "SDPhotoBrowser.h"
#import "MM_AlertView.h"

static const CGFloat addBtnTag = 100;
static const CGFloat heightForCell = 26;
static const NSInteger maxCountOfRow = 6;
static const NSInteger AddTag = 100000;
static const NSInteger MaxPicCount = 20;

#define max_X 40

@interface MM_CodeResultIncreaseDecreaseAlertView ()<UITextFieldDelegate,UITextViewDelegate,TZImagePickerControllerDelegate,SDPhotoBrowserDelegate>

@property (nonatomic, strong) UIView *maskView;
@property (nonatomic, strong) UIView *alertView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextField *numberTextField;
@property (nonatomic, strong) UILabel *descLabel;
@property (nonatomic, strong) UIView *descView;
@property (nonatomic, strong) Custom_PlaceholderTextView *textView;
@property (nonatomic, strong) UIButton *cancleBtn;
@property (nonatomic, strong) UIButton *fitBtn;
@property (nonatomic, copy) NSString *resonStr;
@property (nonatomic, strong) UIView *picView;
@property (nonatomic, strong) UIButton *addPicBtn;
@property (nonatomic, assign) CGFloat desc_H;
@property (nonatomic, strong) NSMutableArray *image_Arr;
@property (nonatomic, copy) NSString *remark_str;
@property (nonatomic, copy) NSString *number_str;
@property (nonatomic, copy) NSString *reson_id;

@end

@implementation MM_CodeResultIncreaseDecreaseAlertView

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)addNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillHideNotification object:nil];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
        
        self.desc_H = 40;
        
        _maskView = [[UIView alloc] init];
        _maskView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5];
        [self addSubview:_maskView];
        
        _alertView = [[UIView alloc] init];
        _alertView.backgroundColor = MM_ColorUtil(@"#ffffff");
        _alertView.layer.cornerRadius = 10;
        _alertView.layer.masksToBounds = YES;
        [self addSubview:_alertView];

        _titleLabel = [Custom_ZHTool uiLabelCreate:@"减少或增加库存" Font:FONT_BOLD(16) TextColor:[ColorUtil colorWithHexadecimal:@"#000000"]];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.alertView addSubview:_titleLabel];
        
        _numberTextField = [[UITextField alloc] init];
        _numberTextField.delegate = self;
        _numberTextField.textAlignment = NSTextAlignmentLeft;
        _numberTextField.keyboardType = UIKeyboardTypeNumberPad;
        _numberTextField.font = FONT(14);
        _numberTextField.placeholder = @"请输入调整数量";
        _numberTextField.textColor = [ColorUtil colorWithHexadecimal:@"#333333"];
        [_numberTextField addTarget:self action:@selector(numberTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        _numberTextField.layer.borderColor = [ColorUtil colorWithHexadecimal:@"#333333"].CGColor;
        _numberTextField.layer.borderWidth = 0.5;
        _numberTextField.layer.cornerRadius = 5;
        _numberTextField.layer.masksToBounds = YES;
        [self.alertView addSubview:_numberTextField];

        _descLabel = [Custom_ZHTool uiLabelCreate:@"请选择和填写调整原因(图片)" Font:FONT(14) TextColor:[ColorUtil colorWithHexadecimal:@"#000000"]];
        _descLabel.textAlignment = NSTextAlignmentLeft;
        [self.alertView addSubview:_descLabel];

        _descView = [[UIView alloc] init];
        _descView.backgroundColor = MM_ColorUtil(@"#ffffff");
        [self.alertView addSubview:_descView];
        
        _textView = [[Custom_PlaceholderTextView alloc] init];
//        _textView.limitNum = 500;
        _textView.delegate = self;
        _textView.myPlaceholderColor = [ColorUtil colorWithHexadecimal:@"#999999"];
        _textView.myPlaceholder = @"请输入备注";
        _textView.textAlignment = NSTextAlignmentLeft;
        _textView.layer.borderColor = [ColorUtil colorWithHexadecimal:@"#333333"].CGColor;
        _textView.layer.borderWidth = 0.5;
        _textView.layer.cornerRadius = 5;
        _textView.layer.masksToBounds = YES;
        [self.alertView addSubview:_textView];
        
        _picView = [[UIView alloc] init];
        _picView.backgroundColor = MM_ColorUtil(@"#ffffff");
        [self.alertView addSubview:_picView];

        _addPicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addPicBtn setImage:[UIImage imageNamed:@"addUploadPicImage"] forState:UIControlStateNormal];
        _addPicBtn.layer.cornerRadius = 10;
        _addPicBtn.layer.masksToBounds = YES;
        _addPicBtn.layer.borderColor = [ColorUtil colorWithHexadecimal:@"#707070"].CGColor;
        _addPicBtn.layer.borderWidth = 0.5;
        _addPicBtn.hidden = NO;
        [_addPicBtn addTarget:self action:@selector(btnPicClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.picView addSubview:_addPicBtn];

        _cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancleBtn MM_ConfigButtonWithTitle:@"取消" titleFont:FONT(16) titleColor:[ColorUtil colorWithHexadecimal:@"#1F71C4"] bgColor:[ColorUtil colorWithHexadecimal:@"#ffffff"] cornerRadius:10 borderColor:nil borderWidth:0];
        [_cancleBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _cancleBtn.tag = 100;
        [self.alertView addSubview:_cancleBtn];
        
        _fitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_fitBtn MM_ConfigButtonWithTitle:@"确定" titleFont:FONT_BOLD(16) titleColor:[ColorUtil colorWithHexadecimal:@"#1F71C4"] bgColor:[ColorUtil colorWithHexadecimal:@"#ffffff"] cornerRadius:10 borderColor:nil borderWidth:0];
        [_fitBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _fitBtn.tag = 101;
        [self.alertView addSubview:_fitBtn];

        [self setUpViews];
        [self addNotification];
    }
    return self;
}

- (void)setUpViews {
    
    [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self);
    }];
    
    [self.alertView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(max_X);
        make.right.equalTo(-max_X);
        make.centerX.centerY.equalTo(self);
        make.height.equalTo(360);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(20);
        make.centerX.equalTo(self.alertView);
    }];
    
    [self.numberTextField mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(10);
        make.right.equalTo(-10);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(20);
        make.height.equalTo(30);
    }];
    
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(10);
        make.right.equalTo(-10);
        make.top.equalTo(self.numberTextField.mas_bottom).offset(10);
    }];
    
    [self.descView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.equalTo(self.alertView);
        make.top.equalTo(self.descLabel.mas_bottom).offset(10);
        make.height.equalTo(self.desc_H);
    }];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(10);
        make.right.equalTo(-10);
        make.top.equalTo(self.descView.mas_bottom).offset(10);
        make.height.equalTo(70);
    }];
    
    CGFloat space = 10;
    CGFloat picY = 10;
    
    CGFloat relationSubViewW = ((ScreenW - max_X*2) - space*(maxCountOfRow+1))/maxCountOfRow;
    CGFloat relationSubViewH = relationSubViewW;

    [self.picView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self.alertView);
        make.top.equalTo(self.textView.mas_bottom).offset(10);
        make.height.equalTo(60);
    }];
    
    _addPicBtn.hidden = (self.image_Arr.count == MaxPicCount);
    NSInteger btnCount = _addPicBtn.hidden?self.image_Arr.count-1:self.image_Arr.count;
    CGRect addBtnFrame = CGRectMake(12 + (relationSubViewW + space)*(btnCount%6), picY+(relationSubViewH + space)*(btnCount/6), relationSubViewW, relationSubViewH);
    _addPicBtn.frame = addBtnFrame;

//    [self.addPicBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//       
//        make.left.equalTo(10);
//        make.top.equalTo(self.picView);
//        make.width.height.equalTo(40);
//    }];
       
    [self.cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.equalTo(self.alertView);
        make.left.equalTo(self.alertView);
        make.width.equalTo((ScreenW - max_X*2)/2.0);
        make.height.equalTo(44);
    }];
    
    [self.fitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.equalTo(self.alertView);
        make.left.equalTo(self.cancleBtn.mas_right);
        make.width.equalTo((ScreenW - max_X*2)/2.0);
        make.height.equalTo(44);
    }];
    
}

- (void)setTitleStr:(NSString *)titleStr {
    _titleStr = titleStr;
    
    self.titleLabel.text = titleStr;
}

- (void)setResonArr:(NSArray *)resonArr {
    _resonArr = resonArr;
    
    CGFloat tagBtnX = 10;
    CGFloat tagBtnY = 0;
    
    for (UIView *subView in self.descView.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            [subView removeFromSuperview];
        }
    }
    
    for (int i= 0; i<resonArr.count; i++) {
        
        MM_CodeResultResonModel *model = resonArr[i];

        CGSize tagTextSize = [model.dict_value sizeWithFont:[UIFont fontWithName:@"PingFang SC" size:14] maxSize:CGSizeMake(ScreenW-max_X*2, heightForCell)];
        
        if (tagBtnX+tagTextSize.width+heightForCell > ScreenW-max_X*2) {
            
            tagBtnX = 10;
            tagBtnY += heightForCell+(heightForCell/2);
        }
        UIButton *tagBtn = [self.descView viewWithTag:addBtnTag+i];
        
        if (tagBtn == nil) {
            tagBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            tagBtn.titleLabel.font = [UIFont fontWithName:@"PingFang SC" size:14];
            [tagBtn addTarget:self action:@selector(tagBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            tagBtn.tag = addBtnTag+i;
            tagBtn.layer.cornerRadius = 4;
            tagBtn.layer.masksToBounds = YES;
            tagBtn.layer.borderWidth = 0.5;
            tagBtn.layer.borderColor = [ColorUtil colorWithHexadecimal:@"#333333"].CGColor;
            [tagBtn setTitle:model.dict_value forState:UIControlStateNormal];
            tagBtn.backgroundColor = [ColorUtil colorWithHexadecimal:@"#ffffff"];
            [tagBtn setTitleColor:[ColorUtil colorWithHexadecimal:@"#333333"] forState:UIControlStateNormal];
            [self.descView addSubview:tagBtn];
            
        }else {
            tagBtn.hidden = NO;
        }
        
        if ([model.dict_value isEqualToString:self.resonStr]) {
            
            tagBtn.backgroundColor = [ColorUtil colorWithHexadecimal:@"#1F71C4"];
            [tagBtn setTitleColor:[ColorUtil colorWithHexadecimal:@"#ffffff"] forState:UIControlStateNormal];
        } else {
            tagBtn.backgroundColor = [ColorUtil colorWithHexadecimal:@"#ffffff"];
            [tagBtn setTitleColor:[ColorUtil colorWithHexadecimal:@"#333333"] forState:UIControlStateNormal];
        }
        tagBtn.frame = CGRectMake(tagBtnX, tagBtnY, tagTextSize.width+heightForCell, heightForCell);
        
        tagBtnX = CGRectGetMaxX(tagBtn.frame)+12;
    }
    
    self.desc_H = (tagBtnY + heightForCell) + 10;
    
    [self.descView mas_updateConstraints:^(MASConstraintMaker *make) {
       
        make.height.equalTo(self.desc_H);
    }];
    
    [self.alertView mas_updateConstraints:^(MASConstraintMaker *make) {
       
//        make.left.equalTo(max_X);
//        make.right.equalTo(-max_X);
//        make.centerX.centerY.equalTo(self);
        
        if (self.desc_H > 40) {
            make.height.equalTo(320 + self.desc_H);
        } else {
            make.height.equalTo(360);
        }
    }];
    
}

- (void)tagBtnClick:(UIButton *)tagBtn {
    
    NSInteger index = tagBtn.tag - addBtnTag;
    
    for (UIView *subView in self.descView.subviews) {
        
        if ([subView isKindOfClass:[UIButton class]]) {
            
            UIButton *button = (UIButton *)subView;
            button.backgroundColor = [ColorUtil colorWithHexadecimal:@"#ffffff"];
            [button setTitleColor:[ColorUtil colorWithHexadecimal:@"#333333"] forState:UIControlStateNormal];
            button.layer.borderWidth = 0.5;
            button.layer.borderColor = [ColorUtil colorWithHexadecimal:@"#333333"].CGColor;
        }
    }
    
    tagBtn.backgroundColor = [ColorUtil colorWithHexadecimal:@"#1F71C4"];
    [tagBtn setTitleColor:[ColorUtil colorWithHexadecimal:@"#ffffff"] forState:UIControlStateNormal];
    tagBtn.layer.borderWidth = 0;
    tagBtn.layer.borderColor = [UIColor clearColor].CGColor;

    MM_CodeResultResonModel *model = self.resonArr[index];
    self.resonStr = model.dict_value;
    self.reson_id = model.reson_id;
}

- (void)btnPicClick:(UIButton *)button {
    
    self.alpha = 0;
    
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc]initWithMaxImagesCount:MaxPicCount - self.image_Arr.count delegate:self];
    imagePickerVc.allowPickingGif = NO;
    imagePickerVc.allowPickingOriginalPhoto = YES;
    imagePickerVc.allowPickingMultipleVideo = NO;
    imagePickerVc.allowTakeVideo = NO;
    imagePickerVc.allowTakePicture = YES;
    imagePickerVc.barItemTextColor = [ColorUtil colorWithHexadecimal:@"#333333"];
    if (@available(iOS 11.0, *)) {
        imagePickerVc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    } else {
    }
    [imagePickerVc setImagePickerControllerDidCancelHandle:^{
        self.alpha = 1;
    }];
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
        self.alpha = 1;
        
        for (int i=0; i<photos.count; i++) {
            
            UIImage *image = photos[i];
            PHAsset *asset = assets[i];

            NSString *fileName = [asset valueForKey:@"filename"];
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *filePath = [[paths objectAtIndex:0]stringByAppendingPathComponent:fileName];  // 保存文件的名称
            BOOL result = [UIImagePNGRepresentation(image)writeToFile:filePath atomically:YES];
                
            if (result) {
                
                MM_UploadImageModel *imageModel = [[MM_UploadImageModel alloc] init];
                imageModel.canUploadImage = NO;
                imageModel.tempImage = image;
                imageModel.file_path = filePath;
                [self.image_Arr addObject:imageModel];
            }
        }
        
        [self setUpImageArr:self.image_Arr];
        
        //主线程刷新ui
        dispatch_async(dispatch_get_main_queue(), ^(){
            
//            [weakSelf.tableView reloadData];
        });
    }];
                    
    [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:imagePickerVc animated:YES completion:nil];

}

- (void)setUpImageArr:(NSArray *)imageArr {
    
    [self uploadTagValue];
    [self uploadSubView];
    
    CGFloat space = 10;
    CGFloat picY = 10;
    CGFloat picH = 10;
    
    CGFloat relationSubViewW = ((ScreenW - max_X*2) - space*(maxCountOfRow+1))/maxCountOfRow;
    CGFloat relationSubViewH = relationSubViewW;

    NSInteger count = imageArr.count;
    
    for (UIView *subView in self.picView.subviews) {
        
        if ([subView isKindOfClass:[MM_UploadPicView class]]) {
            [subView removeFromSuperview];
        }
    }
    
    if (count > 0) {
        
        for (NSInteger i=0; i<count; i++) {
            MM_UploadImageModel *addImageModel = imageArr[i];
            MM_UploadPicView *UploadPicView = (MM_UploadPicView *)[self.picView viewWithTag:i + AddTag];
            if (!UploadPicView) {
                UploadPicView = [[MM_UploadPicView alloc] init];
                UploadPicView.tag = i+AddTag;
                UploadPicView.layer.cornerRadius = 10;
                UploadPicView.layer.masksToBounds = YES;
                [self.picView addSubview:UploadPicView];
                //回调block
                [self addBlockWithPicView:UploadPicView];
            }
            UploadPicView.frame = CGRectMake(12 + (relationSubViewW + space)*(i%6), picY+(relationSubViewH + space)*(i/6), relationSubViewW, relationSubViewH);
            if (addImageModel) {
                if (addImageModel.canUploadImage) {
                    if (addImageModel.tempImage) {
                        addImageModel.canUploadImage = NO;
                        UploadPicView.imageModel = addImageModel;
                        [UploadPicView startUploadPic];
                    }else {
                        UploadPicView.imageModel = addImageModel;
                    }
                }else {
                    UploadPicView.imageModel = addImageModel;
                }
            }
        }
    }
    
    _addPicBtn.hidden = (count == MaxPicCount);
    NSInteger btnCount = _addPicBtn.hidden?count-1:count;
    CGRect addBtnFrame = CGRectMake(12 + (relationSubViewW + space)*(btnCount%6), picY+(relationSubViewH + space)*(btnCount/6), relationSubViewW, relationSubViewH);
    _addPicBtn.frame = addBtnFrame;
        
    [self.picView mas_updateConstraints:^(MASConstraintMaker *make) {
       
        make.height.equalTo(CGRectGetMaxY(addBtnFrame) + picH);
    }];
    
    [self.alertView mas_updateConstraints:^(MASConstraintMaker *make) {
       
//        make.left.equalTo(max_X);
//        make.right.equalTo(-max_X);
//        make.centerX.centerY.equalTo(self);
        
        make.height.equalTo(320 + self.desc_H + CGRectGetMaxY(addBtnFrame) + picH);
    }];
}

/*******重新计算tag值**********/
- (void)uploadTagValue {
    
    NSInteger add = 0 ;
    for (UIView * subView in self.picView.subviews) {
        if ([subView isKindOfClass:[MM_UploadPicView class]]) {
            MM_UploadPicView *jhUploadPic = (MM_UploadPicView *)subView;
            jhUploadPic.tag = add+AddTag;
            add++;
        }
    }
}

- (void)uploadSubView {
    
    for (UIView *subView in self.picView.subviews) {
        
        if ([subView isKindOfClass:[MM_UploadPicView class]]) {
            
            MM_UploadPicView *uploadPicView = (MM_UploadPicView *)subView;
            [uploadPicView removeFromSuperview];
        }
    }
}

- (void)addBlockWithPicView:(MM_UploadPicView *)jhUploadPic {
    
    __weak typeof(self)weakSelf = self;
    __weak MM_UploadPicView *weakPicView = jhUploadPic;
    [jhUploadPic setSuccessBlock:^(NSDictionary * _Nonnull userImageModel) {
        //通过回调的数据再次发送请求
        NSLog(@"responseDic = %@",userImageModel);
    } failBlock:^(NSString *message) {
        NSLog(@"message = %@",message);
        [MM_PublicHUD showStateText:message];
        
    } tapPicBlock:^{
        NSLog(@"点击图片");
        
        SDPhotoBrowser *photoBrowser = [[SDPhotoBrowser alloc]init];
        photoBrowser.delegate = weakSelf;
        photoBrowser.currentImageIndex = weakPicView.tag - AddTag;
        photoBrowser.imageCount = weakSelf.image_Arr.count;
        photoBrowser.sourceImagesContainerView = weakSelf.picView;
        [photoBrowser show];
        
    } deletePicBlock:^(MM_UploadPicView * picView){
        NSLog(@"删除图片");
        [picView clearnPicViewRequest];
        
        [MM_AlertView MM_AlertWithTitle:@"" message:@"是否删除图片?" withJH_AlertBlock:^(MM_AlertViewBtnType buttonIndexType) {
      
            
            if (buttonIndexType == MM_AlertViewBtnTypeEnsure) {
                [weakSelf deletePicView:picView];
            }
        } cancelButtonTitle:@"取消" ensureButtonTitle:@"确定"];
        
    } errorPicBlock:^{
        NSLog(@"出错点击图片");
        
        [MM_AlertView MM_AlertWithTitle:@"" message:@"图片上传失败，是否重新上传" withJH_AlertBlock:^(MM_AlertViewBtnType buttonIndexType) {
            
            if (buttonIndexType == MM_AlertViewBtnTypeEnsure) {
                [weakPicView startUploadPic];
            }
        } cancelButtonTitle:@"取消" ensureButtonTitle:@"确定"];
        
    }];
}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
    
    MM_UploadImageModel *imageModel = self.image_Arr[index];
    return imageModel.tempImage;
}

- (void)deletePicView:(MM_UploadPicView *)picView {
    [self.image_Arr removeObject:picView.imageModel];
    [picView removeFromSuperview];
    [self setUpImageArr:self.image_Arr];
}

- (void)numberTextFieldDidChange:(UITextField *)textField {
    
    self.number_str = textField.text;
}

- (void)textViewDidChange:(UITextView *)textView {
    
    self.remark_str = textView.text;
}

#pragma mark --- 键盘将要出现
- (void)keyboardWillShow:(NSNotification *)notification {
    
    NSDictionary * userInfo = [notification userInfo];
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardEndFrame;
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
    
    if (notification.name == UIKeyboardWillShowNotification) {
      [UIView animateWithDuration:animationDuration animations:^{
        // 显示
        CGFloat heighOfOffY = keyboardEndFrame.size.height + 1;
                    
//        UIEdgeInsets e = UIEdgeInsetsMake(0, 0, heighOfOffY - 70, 0);
          
          [self.alertView mas_updateConstraints:^(MASConstraintMaker *make) {
             
              make.centerY.equalTo(self).offset(-(heighOfOffY - 200));
          }];
          
//        [self.tableView setContentInset:e];// 相当于 tableView 的 底部提高了 一个键盘的 高度
      }completion:^(BOOL finished) {
        
      }];
    }else {
      [UIView animateWithDuration:animationDuration animations:^{
        UIEdgeInsets e = UIEdgeInsetsZero;
          
          [self.alertView mas_updateConstraints:^(MASConstraintMaker *make) {
             
              make.centerX.centerY.equalTo(self);
          }];
//        [self.tableView setContentInset:e];
      }];
    }
}

- (NSMutableArray *)image_Arr {
    
    if (_image_Arr == nil
        ) {
        
        _image_Arr = [[NSMutableArray alloc] init];
    }
    return _image_Arr;
}

- (void)show {
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}
- (void)hide {
    [self removeFromSuperview];
}

- (void)btnClick:(UIButton *)button {
    
    NSInteger index = button.tag - 100;
    
    if (index == 0) {
        [self hide];
        return;
    }
    
    if (stringIsEmpty(self.number_str)) {
        [MM_PublicHUD showStateText:@"请输入调整数量!"];
        return;
    }
    
    if (stringIsEmpty(self.resonStr)) {
        [MM_PublicHUD showStateText:@"请选择调整原因!"];
        return;
    }

    if (self.alertBlock) {
        self.alertBlock(index, self.remark_str,self.number_str, self.reson_id, self.image_Arr);
    }
    
    [self cancleData];
        
    [self hide];
}

- (void)cancleData {
    
    self.numberTextField.text = @"";
    self.number_str = @"";
        
    self.reson_id = @"";
    self.resonStr = @"";
    
    self.textView.text = @"";
    self.remark_str = @"";
    
    CGFloat space = 10;
    CGFloat picY = 10;
    
    CGFloat relationSubViewW = ((ScreenW - max_X*2) - space*(maxCountOfRow+1))/maxCountOfRow;
    CGFloat relationSubViewH = relationSubViewW;
    
    [self.image_Arr removeAllObjects];
    
    _addPicBtn.hidden = (self.image_Arr.count == MaxPicCount);
    NSInteger btnCount = _addPicBtn.hidden?self.image_Arr.count-1:self.image_Arr.count;
    CGRect addBtnFrame = CGRectMake(12 + (relationSubViewW + space)*(btnCount%6), picY+(relationSubViewH + space)*(btnCount/6), relationSubViewW, relationSubViewH);
    _addPicBtn.frame = addBtnFrame;

    [self uploadSubView];
}

@end
